package com.lesama.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receiver {
    public static void main(String[] args) throws IOException {
        // 1. 创建DatagramSocket对象
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        // 2. 创建DatagramPacket对象
        DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024);
        // 3. 接收数据
        datagramSocket.receive(datagramPacket);
        // 4. 对数据拆包
        byte[] data = datagramPacket.getData();
        String s = new String(data, 0, datagramPacket.getLength());
        System.out.println(s);
        // 5. 关闭DatagramSocket
        datagramSocket.close();
    }
}
